<?php
	namespace Api\Controller;
	use Think\Controller;
	require_once './qiniu/autoload.php';
	use Qiniu\Auth;
	use Qiniu\Storage\UploadManager;
	
	use Qiniu\Storage\BucketManager;
	header("Content-Type:text/html;charset=utf-8");
	class UploadtokenController extends Controller
	{
		//向前端传送token值
		public function uploadToken($uid=null, $token=null)
		{
			if ($this->checkedToken($uid, $token)) 
			{
				$bucket='show';
				$expires = 3600;
				$accessKey='-JLvYg-7Pa1SGcLzw1qyrzSt5dqpp-7Xr5ZB_XI3';
				$secretKey='p0srnaa2qyYM7fS5kKp_OCUTYwiUMb-Jr8C9VXxY';
				$auth = new Auth($accessKey, $secretKey);
				$upToken = $auth->uploadToken($bucket, null, time()+3600);//获取上传所需的token
				
				
				var_dump($upToken);
				/* $client = new QiniuClient($accessKey,$secretKey);
				 $flags = array();
				$scope = $bucket;
				$deadline = time() + $expires;
				$flags['scope'] = $scope;
				$flags['deadline'] = $deadline;
				$flags['returnBody'] = null;
				echo $client->uploadToken($flags); */
			}
			else
			{
				$array1['result'] = 10310;
				$array1['message'] = "获取信息失败";
				echo json_encode($array1, JSON_UNESCAPED_SLASHES);
				exit;
			}
			
		}
		
		/*
		 *验证token的方法
		 *
		 */
		protected function checkedToken($uid = null,$token = null)
		{
			if ($uid==null || $token==null)
			{
				return false;
			}
			else
			{
				$where["uid"] = $uid;
				$tokeninfo = M("User") -> where($where) -> getField("token");
				if ($token == $tokeninfo)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
	}